/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.tribes.reportserver.ui;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.DateField;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import de.tor.tribes.reportserver.util.UIUtil;
import de.tor.tribes.reportserver.util.ReportFilterHelper;
import de.tor.tribes.types.ZebraReport;
import java.util.Date;
import de.tor.tribes.types.ZebraReportGroup;
import de.tor.tribes.types.ZebraUser;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Torridity
 */
public class SearchScreen extends CustomComponent {

    private GridLayout mainLayout;
    private TextField sourceField;
    private TextField targetField;
    private TextField attackerField;
    private TextField defenderField;
    private CheckBox greenBox;
    private CheckBox yellowBox;
    private CheckBox redBox;
    private CheckBox blueBox;
    private CheckBox redYellowBox;
    private CheckBox redBlueBox;
    private DateField fromField;
    private DateField toField;
    private CheckBox acceptanceChangeField;
    private CheckBox justOwnField;
    private Button searchButton;
    private Button resetButton;

    public SearchScreen() {
        buildMainLayout();
        setCompositionRoot(mainLayout);
    }

    private void buildMainLayout() {
        fromField = new DateField("Von");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, 2010);
        fromField.setValue(c.getTime());
        fromField.setDateFormat("dd.MM.yy HH:mm");
        fromField.setResolution(DateField.RESOLUTION_MIN);
        fromField.setWidth("100%");
        toField = new DateField("Bis");
        toField.setValue(new Date());
        toField.setDateFormat("dd.MM.yy HH:mm");
        toField.setResolution(DateField.RESOLUTION_MIN);
        toField.setWidth("100%");
        sourceField = new TextField("Herkunft");
        sourceField.setNullRepresentation("");
        sourceField.setWidth("100%");
        targetField = new TextField("Ziel");
        targetField.setNullRepresentation("");
        targetField.setWidth("100%");
        attackerField = new TextField("Angreifer");
        attackerField.setNullRepresentation("");
        attackerField.setWidth("100%");
        defenderField = new TextField("Verteidiger");
        defenderField.setNullRepresentation("");
        defenderField.setWidth("100%");
        greenBox = new CheckBox();
        greenBox.setIcon(ZebraReport.REPORT_COLOR.GREEN.getIcon());
        yellowBox = new CheckBox();
        yellowBox.setIcon(ZebraReport.REPORT_COLOR.YELLOW.getIcon());
        redBox = new CheckBox();
        redBox.setIcon(ZebraReport.REPORT_COLOR.RED.getIcon());
        blueBox = new CheckBox();
        blueBox.setIcon(ZebraReport.REPORT_COLOR.BLUE.getIcon());
        redYellowBox = new CheckBox();
        redYellowBox.setIcon(ZebraReport.REPORT_COLOR.RED_YELLOW.getIcon());
        redBlueBox = new CheckBox();
        redBlueBox.setIcon(ZebraReport.REPORT_COLOR.RED_BLUE.getIcon());
        acceptanceChangeField = new CheckBox();
        acceptanceChangeField.setDescription("Berichte mit Zustimmungsänderung");
        acceptanceChangeField.setIcon(new ThemeResource("../zebra/img/units/snob.png"));
        justOwnField = new CheckBox("Nur eigene Berichte");
        searchButton = new Button("Berichte suchen");
        searchButton.setWidth("100%");
        resetButton = new Button("Zurücksetzen");
        resetButton.setWidth("100%");
        HorizontalLayout colorLayout = new HorizontalLayout();
        colorLayout.setCaption("Ignorierte Berichttypen");
        colorLayout.addComponent(blueBox);
        colorLayout.addComponent(greenBox);
        colorLayout.addComponent(yellowBox);
        colorLayout.addComponent(redBox);
        colorLayout.addComponent(redBlueBox);
        colorLayout.addComponent(redYellowBox);
        colorLayout.setWidth("100%");

        HorizontalLayout miscLayout = new HorizontalLayout();
        miscLayout.setCaption("Sonstiges");
        miscLayout.setWidth("100%");
        miscLayout.addComponent(acceptanceChangeField);
        miscLayout.addComponent(justOwnField);

        mainLayout = new UIUtil.GridLayoutBuilder(4, 6)
                .addComponent(fromField, 0, 0, 2, 1).
                addComponent(toField, 2, 0, 2, 1)
                .addComponent(attackerField, 0, 1, 2, 1).
                addComponent(defenderField, 2, 1, 2, 1)
                .addComponent(sourceField, 0, 2, 2, 1).
                addComponent(targetField, 2, 2, 2, 1)
                .addComponent(colorLayout, 0, 3, 4, 1)
                .addComponent(miscLayout, 0, 4, 4, 1)
                .addComponent(searchButton, 0, 5, 2, 1).addComponent(resetButton, 2, 5, 2, 1)
                .getLayout();
        mainLayout.setMargin(true);
        mainLayout.setSpacing(true);
        mainLayout.setSizeFull();
    }

    private List<ZebraReport.REPORT_COLOR> getIgnoredColors() {
        List<ZebraReport.REPORT_COLOR> colors = new LinkedList<ZebraReport.REPORT_COLOR>();

        if (greenBox.booleanValue()) {
            colors.add(ZebraReport.REPORT_COLOR.GREEN);
        }
        if (yellowBox.booleanValue()) {
            colors.add(ZebraReport.REPORT_COLOR.YELLOW);
        }
        if (redBox.booleanValue()) {
            colors.add(ZebraReport.REPORT_COLOR.RED);
        }
        if (blueBox.booleanValue()) {
            colors.add(ZebraReport.REPORT_COLOR.BLUE);
        }
        if (redYellowBox.booleanValue()) {
            colors.add(ZebraReport.REPORT_COLOR.RED_YELLOW);
        }
        if (redBlueBox.booleanValue()) {
            colors.add(ZebraReport.REPORT_COLOR.RED_BLUE);
        }
        return colors;
    }

    public String getReportsQuery(ZebraUser pUser) {
        return ReportFilterHelper.getFindReportsQuery(pUser, (Date) fromField.getValue(),
                (Date) toField.getValue(),
                (String) attackerField.getValue(),
                (String) sourceField.getValue(),
                (String) defenderField.getValue(),
                (String) targetField.getValue(),
                getIgnoredColors(), acceptanceChangeField.booleanValue(), justOwnField.booleanValue());
    }

    public String getReportsQuery(ZebraUser pUser, ZebraReportGroup pGroup) {
        return ReportFilterHelper.getFindReportsQuery(pUser, pGroup, (Date) fromField.getValue(),
                (Date) toField.getValue(),
                (String) attackerField.getValue(),
                (String) sourceField.getValue(),
                (String) defenderField.getValue(),
                (String) targetField.getValue(),
                getIgnoredColors(), acceptanceChangeField.booleanValue(), justOwnField.booleanValue());
    }

    public String getReportCountQuery(ZebraUser pUser) {
        return ReportFilterHelper.getCountReportQuery(pUser, (Date) fromField.getValue(),
                (Date) toField.getValue(),
                (String) attackerField.getValue(),
                (String) sourceField.getValue(),
                (String) defenderField.getValue(),
                (String) targetField.getValue(),
                getIgnoredColors(), acceptanceChangeField.booleanValue(), justOwnField.booleanValue());
    }

    public String getReportCountQuery(ZebraUser pUser, ZebraReportGroup pGroup) {
        return ReportFilterHelper.getCountReportQueryByGroup(pUser, pGroup, (Date) fromField.getValue(),
                (Date) toField.getValue(),
                (String) attackerField.getValue(),
                (String) sourceField.getValue(),
                (String) defenderField.getValue(),
                (String) targetField.getValue(),
                getIgnoredColors(), acceptanceChangeField.booleanValue(), justOwnField.booleanValue());
    }
}
